from PIL import Image
from tensorbay import GAS
from tensorbay.dataset import Dataset
import numpy as np
import csv

gas = GAS("Accesskey-ee75f1427dbca536bedc3bbec492f5ce")

dataset = Dataset("RP2K", gas)

lables = {}
index = [1]

print(dataset.keys())

train_segment = dataset["train"]
test_segment = dataset["test"]

print(len(train_segment))
print(len(test_segment))

def preprocess(dataset,index):
    for image in dataset:
        if not lables.__contains__(image.label.classification.category):
            lables[image.label.classification.category] = index[0]
            index[0] = index[0] + 1


preprocess(train_segment,index)

out = open('lables_csv.csv','a', newline='')
csv_write = csv.writer(out,dialect='excel')

for (key,value) in zip(lables.keys(),lables.values()):
    csv_write.writerow([key,value])

print(len(lables.keys()))
print(lables.keys())

# for image in segment:
#     print(image.label.classification.category)

# for i in range(len(segment)):
#     with segment[i].open() as fp:
#         image = Image.open(fp)
#         width, height = image.size
#         print(image)
#         print(width,height)
#         print(segment[i].label)
#         image.show()
#         im_array = np.array(image)
#         print(im_array,im_array.shape)